package com.liuf.di.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuf.di.api.ServiceException;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.repository.IDatabaseRepository;
import com.liuf.di.service.IDatabaseService;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 元数据业务管理
 * @author: Mr.Liu
 * @create: 2018-09-04 15:03
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Service
public class DatabaseServiceImpl implements IDatabaseService {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseServiceImpl.class);

    private ObjectId objectId = null;

    /**Kettle资源库*/
    private Repository repository;

    private DatabaseMeta databaseMeta;

    @Autowired
    private KettleInitFactory kettleInitFactory;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private IDatabaseRepository databaseRepository;

    @Override
    public PageInfo getDatabases(Map<String,Object> params) throws ServiceException {
        PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
        return new PageInfo(databaseRepository.getDatabases(params));
    }

    @Override
    public String save(Map<String, String> param) throws ServiceException {
       return this.saveUpdate(param);
    }

    @Override
    public Map<String, Object> getDatabase(long id) throws ServiceException {
        return databaseRepository.getDatabase(id);
    }

    @Override
    public boolean detele(String name) throws ServiceException {
        repository = kettleInitFactory.getRepositoryInit();
        objectId = null;
        try {
            objectId = repository.getDatabaseID(name);
            ((KettleDatabaseRepository) repository).databaseDelegate.delDatabase(objectId);
            List<DatabaseMeta> databaseMetas = repository.readDatabases();
            String objectId = DatabaseServiceImpl.getObjectId(name,repository);
            return objectId==null?true:false;
        } catch (KettleException e) {
            e.printStackTrace();
            return false;
        }finally {
            if(repository != null){
                repository.disconnect();
            }
        }
    }

    @Override
    public List<Map<String, Object>> getDatabaseAttribute(Map<String, Object> params) throws ServiceException {
        return databaseRepository.getDatabaseAttribute(params);
    }

    @Override
    public List<Map<String, Object>> getDatabaseContype(Map<String, Object> params) throws ServiceException {
        return databaseRepository.getDatabaseContype(params);
    }

    @Override
    public List<Map<String, Object>> getDatabaseType(Map<String, Object> params) throws ServiceException {
        return databaseRepository.getDatabaseType(params);
    }

    @Override
    public Boolean booleanDataBase(String name) throws ServiceException {
        return databaseRepository.booleanDataBase(name);
    }

    /**
     * 存储和修改数据元
     * @param param 数据参数
     * @return  objectID
     */
    public String saveUpdate(Map<String,String> param){
        databaseMeta = new DatabaseMeta();
        repository = kettleInitFactory.getRepositoryInit();
        objectId = null;
        try {
            /**初始化元数据资源库*/
            databaseMeta = new DatabaseMeta(param.get("name"), param.get("type"), param.get("access"), param.get("host"), param.get("dbname"), param.get("port"), param.get("user"), param.get("pass"));
            databaseMeta.setForcingIdentifiersToLowerCase(true);
            repository.save(databaseMeta, null, null,true);
            return DatabaseServiceImpl.getObjectId(param.get("name"),repository);
        } catch (KettleException e) {
            e.printStackTrace();
            logger.error("[数据元新增||修改操作]失败！");
            return null;
        }finally {
            /**释放kettle资源资源*/
            if(kettleInitFactory !=null) {
                repository.disconnect();
            }
        }
    }


    /***
     * 根据元数据名称获取ObjectId
     * @return  objectId
     */
    public static String getObjectId(String name,Repository repository) throws KettleException {
        return repository.getDatabaseID(name).getId();
    }

}
